﻿// toLuaEasy.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

/* 使用VLD工具检测内存泄露 https://www.huaweicloud.com/articles/12662339.html */
#include "vld.h"

#include <iostream>
#include "LuaVM.h"
#include "LuaDefine.h"
#include "toLuaEasy.h"
#include "../Log/TLog.h"

#include "../toLuaEasy//CustomAction/ArrayValue/UArrayValue.h"


GameApplication* g_LuaApplication = nullptr;

//TODO : 看看这个类在winmain里面哪里调用合适
GameApplication::GameApplication()
{
}

GameApplication::~GameApplication()
{
}

bool GameApplication::Init()
{
	g_LuaApplication = this;
	float array[3] = { 0,1,2 };

	bool bRet = false;
	m_LuaPkgManager = new LuaPkgManager();
	LOG_PROCESS_ERROR(m_LuaPkgManager);

	m_piArrayValue = new TArrayValue();
	LOG_PROCESS_ERROR(m_piArrayValue);

	
	//m_piArrayValue->ShowFloatArray_StableLength(array);

	bRet = true;
Exit0:
	return bRet;
}

void GameApplication::UnInit()
{
	if (m_LuaPkgManager != nullptr)
	{
		delete(m_LuaPkgManager);
	}

	if (m_piArrayValue != nullptr)
	{
		delete(m_piArrayValue);
	}

	g_DestroyLuaVM();

#ifndef CONSOLE_OUT
	LogCache::UnInit();
#endif
}

void GameApplication::LoadScripts()
{
	g_GetLuaVM()->ExecuteScriptFile("../Product/script/src/scripts.lua");

	uint32_t nRetCode = false;
	LuaVM* pVM = g_GetLuaVM();
	lua_State* L = pVM->LuaState();
	LuaSimpleCall0(L, "load_Scripts");
}

bool GameApplication::ApplicationStart()
{
	bool bRet = false;


	if (g_CreateLuaVM())
	{
		lua_State* L = g_GetLuaVM()->LuaState();
		// 打开toLua++提供的lib
		m_LuaPkgManager->OpenLuaPkgLibs(L);
		LoadScripts();
	}

	bRet = true;
Exit0:
	return bRet;
}

GameApplication* GetGameApplication()
{
	return g_LuaApplication;
}
